package com.sky.service.user.impl;

import com.alibaba.fastjson.JSON;
import com.hxc.pay.wx.service.PaySuccessService;
import com.sky.constant.BaseServiceConstant;
import com.sky.constant.PayStatusConstant;
import com.sky.mapper.common.OrdersMapper;
import com.sky.pojo.Orders;
import com.sky.vo.OrderAdviceInfo;
import com.sky.websocket.WebSocketServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * 支付成功回调通知业务实现
 *
 * @author zgp
 * @version V1.0
 * @date 2025-02-03 15:28
 */
@Service
public class PaySuccessServiceImpl implements PaySuccessService {
    @Autowired
    private OrdersMapper ordersMapper;
    @Autowired
    private WebSocketServer webSocketServer;

    @Override
    public void success(String outTradeNo) {
        // 修改订单状态
        Orders orders = Orders.builder()
                .number(outTradeNo)
                .status(Orders.TO_BE_CONFIRMED)
                .checkoutTime(LocalDateTime.now())
                .payStatus(PayStatusConstant.PAID)
                .build();
        ordersMapper.updateByNumber(orders);
        // 来单提醒
        OrderAdviceInfo adviceInfo = new OrderAdviceInfo();
        adviceInfo.setType(BaseServiceConstant.ONE);
        adviceInfo.setOrderId(ordersMapper.selectByIdOrNumber(null, outTradeNo).getId().toString());
        adviceInfo.setContent(outTradeNo);
        webSocketServer.sendReminder(JSON.toJSONString(adviceInfo));
    }
}
